package org.xjtu.weteam.model.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.xjtu.weteam.model.dto.ProjectDateDTO;
import org.xjtu.weteam.model.dto.TaskDateDTO;

import java.sql.Date;
import java.sql.Timestamp;

/**
 * @author Kevin
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Entity
@Table(name = "Task")
public class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer taskId;

    private Integer projectId;
    private Integer pId;
    private String taskName;
    private String description;

    private Date dueDate;

    private String status;
    @ManyToOne
    @JoinColumn(name = "status", referencedColumnName = "code", insertable = false, updatable = false)
    private Dictionary statusDictionary; // 关联 Dictionary 实体

    private String url;

    @Column(name = "createTime", updatable = false)
    private Timestamp createTime;

    @Column(name = "updateTime")
    private Timestamp updateTime;

//    原来的构造函数不能通过
//    public Task(TaskDateDTO taskDateDTO){
//        BeanUtils.copyProperties(taskDateDTO,this);
//        Date due = java.sql.Date.valueOf(taskDateDTO.getDueDate());
//        setDueDate(due);
//    }

    public Task(TaskDateDTO taskDateDTO){
        BeanUtils.copyProperties(taskDateDTO, this);
        String dueDateStr = taskDateDTO.getDueDate();
        if (dueDateStr != null && !dueDateStr.isEmpty()) {
            try {
                Date due = java.sql.Date.valueOf(dueDateStr);
                setDueDate(due);
            } catch (IllegalArgumentException e) {
                // 可以选择抛出自定义异常或记录错误
                throw new IllegalArgumentException("Invalid date format: " + dueDateStr, e);
            }
        } else {
            // 如果dueDateStr为null或空字符串，可以选择设置默认值或保持为null
            setDueDate(null);
        }
    }

}
